package com.http;

import java.io.InputStream;
import java.io.Serializable;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;

import com.interf.Requester;

public class HTTPRequester implements Requester<HTTPResult>, Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private final String requesterHostName;
	
	private String mHostName;
	
	@Override
	public String getHostName() {
		return mHostName;
	}
	
	public HTTPRequester(String address, String requester) {
		this.address = address;
		requesterHostName = requester;
		try {
			mHostName = Inet4Address.getLocalHost().getHostAddress().toString();
		} catch (UnknownHostException e) {
			mHostName = "Unidentified IP";
		}
	}
	private String address;
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Override
	public HTTPResult makeRequest() {
		HTTPResult result = new HTTPResult();
		try {
			long startTime = System.currentTimeMillis();
			URL myURL;
			myURL = new URL(address);
			URLConnection conn = myURL.openConnection();
			result.connectTime = System.currentTimeMillis() - startTime;
			InputStream response = conn.getInputStream();
			result.totalTime = System.currentTimeMillis() - startTime;
			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
